package www.icomwell.com.onepx;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by leeesven on 17/5/4.
 */

public class OnepxActivity extends Activity {

	private BroadcastReceiver br;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Window window = getWindow();
		window.setGravity(Gravity.LEFT | Gravity.TOP);
		WindowManager.LayoutParams params = window.getAttributes();
		params.x = 0;
		params.y = 0;
		params.height = 100;
		params.width = 100;
		window.setAttributes(params);

		br = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
//                LogUtils.i(intent.getAction());
//                LogUtils.i(context.toString());
//                Intent intent1 = new Intent(Intent.ACTION_MAIN);
//                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent1.addCategory(Intent.CATEGORY_HOME);
//                startActivity(intent1);
				Log.d("a","OnepxActivity finish   ================");
				finish();
			}
		};
		registerReceiver(br, new IntentFilter("finish activity"));

		checkScreenOn("onCreate");
	}

	@Override
	protected void onDestroy() {

		Log.i("a","===onDestroy===");
		try{
			unregisterReceiver(br);
		}catch (IllegalArgumentException e){
			Log.e("a","receiver is not resisted: "+e);
		}
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
		checkScreenOn("onResume");
	}

	private void checkScreenOn(String methodName) {
		Log.d("a","from call method: " + methodName);
		PowerManager pm = (PowerManager) OnepxActivity.this.getSystemService(Context.POWER_SERVICE);
		boolean isScreenOn = pm.isScreenOn();
		Log.i("a","isScreenOn: "+isScreenOn);
		if(isScreenOn){
			finish();
		}

	}
}
